package com.qf.ssm.service.Impl;

import com.qf.ssm.entity.order.OrderMessage;
import com.qf.ssm.entity.order.UserAddress;
import com.qf.ssm.entity.user.*;
import com.qf.ssm.ex.ServiceException;
import com.qf.ssm.mapper.UserMapper;
import com.qf.ssm.service.UserService;
import com.qf.ssm.utils.ResponseCode;
import com.qf.ssm.utils.ResponseResult;
import org.junit.rules.ExpectedException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author wzy
 * @Date 0015 2021-02-15 17:19
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;

    /**
     * 管理员登陆方法
     *
     * @param admin
     * @return
     */
    @Override
    public ResponseResult<Admin> adminLogin(Admin admin) {
        ResponseResult<Admin> result;

        try {
            // 接收数据库查询到的用户对象
            Admin admins = userMapper.adminLogin(admin);
            if (admins.getPassword().equals(admin.getPassword())) {
                result = ResponseResult.success(ResponseCode.SUCCESS);
            } else {
                result = ResponseResult.error();
            }
            return result;
        } catch (Exception e) {
            throw new ServiceException(ResponseCode.USER_NOT_NAME);
        }
    }

    /**
     * 用户登陆方法
     *
     * @param user
     * @return
     */
    @Override
    public ResponseResult<User> userLogin(User user) {
        ResponseResult<User> result;

        // 接收数据库查询到的用户对象
        User users = userMapper.userLogin(user);

        try {
            if (users.getPassword().equals(user.getPassword())) {
                result = ResponseResult.success(ResponseCode.SUCCESS);
            } else {
                result = ResponseResult.error();
            }
            return result;
        } catch (Exception e) {
            throw new ServiceException(ResponseCode.USER_NOT_NAME);
        }
    }

    /**
     * 查询普通用户信息
     *
     * @return
     */
    @Override
    public ResponseResult<List<User>> userAllSelect() {
        ResponseResult<List<User>> result;

        try {
            List<User> userList = userMapper.userSelect();
            if (userList != null) {
                result = ResponseResult.success(userList);
            } else {
                result = ResponseResult.error();
            }

            return result;
        } catch (Exception e) {
            throw new ServiceException(ResponseCode.SELECT_ERROR);
        }
    }

    /**
     * 查询VIP用户信息
     *
     * @return
     */
    @Override
    public ResponseResult<List<User>> vipuserAllSelect() {
        ResponseResult<List<User>> result;

        try {
            List<User> vipuserList = userMapper.vipuserSelect();
            if (vipuserList != null) {
                result = ResponseResult.success(vipuserList);
            } else {
                result = ResponseResult.error();
            }
            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.SELECT_ERROR);
        }
    }

    /**
     * 添加普通用户信息
     *
     * @param user
     * @return
     */
    @Override
    public ResponseResult<Integer> userInsert(User user) {
        ResponseResult<Integer> result;
        try {
            int resultCount = userMapper.userInsert(user);

            if (resultCount != 0) {
                result = ResponseResult.success(resultCount);
            } else {
                result = ResponseResult.error();
            }
            return result;
        } catch (Exception e) {
            throw new ServiceException(ResponseCode.INSERT_ERROR);
        }
    }

    /**
     * 添加VIP用户信息
     *
     * @param user
     * @return
     */
    @Override
    public ResponseResult<Integer> vipuserInsert(User user) {
        ResponseResult<Integer> result;

        try {
            int resultCount = userMapper.vipuserInsert(user);

            if (resultCount != 0) {
                result = ResponseResult.success(resultCount);
            } else {
                result = ResponseResult.error();
            }

            return result;
        } catch (Exception e) {
            throw new ServiceException(ResponseCode.INSERT_ERROR);
        }
    }

    /**
     * 查询VIP用户信息
     *
     * @return
     */
    @Override
    public ResponseResult<List<VipLevel>> vipLevelSelect() {
        ResponseResult<List<VipLevel>> result;
        try {
            List<VipLevel> vipLevelList = userMapper.vipLevelSelect();
            if (vipLevelList != null) {
                result = ResponseResult.success(vipLevelList);
            } else {
                result = ResponseResult.error();
            }
            return result;
        } catch (Exception e) {
            throw new ServiceException(ResponseCode.SELECT_ERROR);
        }
    }

    /**
     * 添加VIP等级信息
     *
     * @param vipLevel
     * @return
     */
    @Override
    public ResponseResult<Integer> viplevelInsert(VipLevel vipLevel) {
        ResponseResult<Integer> result;
        try {
            int resultCount = userMapper.viplevelInsert(vipLevel);
            if (resultCount != 0) {
                result = ResponseResult.success(resultCount);
            } else {
                result = ResponseResult.error();
            }
            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.INSERT_ERROR);
        }
    }

    /**
     * 删除VIP等级信息
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult<Integer> viplevelDelete(int id) {
        ResponseResult<Integer> result;

        try {
            int resultCount = userMapper.viplevelDelete(id);
            if (resultCount != 0) {
                result = ResponseResult.success(resultCount);
            } else {
                result = ResponseResult.error();
            }

            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.DELETE_ERROR);
        }
    }

    @Override
    public ResponseResult<Integer> viplevelUpdate(VipLevel vipLevel) {
        ResponseResult<Integer> result;

        try {
            int resultCount = userMapper.viplevelUpdate(vipLevel);
            if (resultCount != 0) {
                result = ResponseResult.success(resultCount);
            } else {
                result = ResponseResult.error();
            }

            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.UPDATE_ERROR);
        }
    }

    /**
     * 删除普通用户信息
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult<Integer> userDelete(int id) {
        ResponseResult<Integer> result;


        try {
            int resultCount = userMapper.userDelete(id);
            if (resultCount != 0) {
                result = ResponseResult.success(resultCount);
            } else {
                result = ResponseResult.error();
            }

            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.DELETE_ERROR);
        }
    }

    /**
     * 删除VIP会员信息
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult<Integer> vipuserDelete(int id) {
        ResponseResult<Integer> result;

        try {
            int resultCount = userMapper.vipuserDelete(id);
            if (resultCount != 0) {
                result = ResponseResult.success(resultCount);
            } else {
                result = ResponseResult.error();
            }
            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.DELETE_ERROR);
        }
    }

    /**
     * 修改普通用户信息
     *
     * @param user
     * @return
     */
    @Override
    public ResponseResult<Integer> userModify(User user) {
        ResponseResult<Integer> result;

        try {
            int resultCount = userMapper.userModify(user);
            if (resultCount != 0) {
                result = ResponseResult.success(resultCount);
            } else {
                result = ResponseResult.error();
            }
            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.UPDATE_ERROR);
        }
    }

    /**
     * 修改VIP会员信息
     *
     * @param user
     * @return
     */
    @Override
    public ResponseResult<Integer> vipuserModify(User user) {
        ResponseResult<Integer> result;

        try {
            int resultCount = userMapper.vipuserModify(user);
            if (resultCount != 0) {
                result = ResponseResult.success(resultCount);
            } else {
                result = ResponseResult.error();
            }
            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.UPDATE_ERROR);
        }
    }

    /**
     * 查询成长模型设置表信息
     *
     * @return
     */
    @Override
    public ResponseResult<List<GrowthSettings>> growthSettingsSelect() {
        ResponseResult<List<GrowthSettings>> result;


        try {
            List<GrowthSettings> growthSettingsList = userMapper.growthSettingsSelect();
            if (growthSettingsList != null) {
                result = ResponseResult.success(growthSettingsList);
            } else {
                result = ResponseResult.error();
            }
            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.SELECT_ERROR);
        }
    }

    /**
     * 添加成长模型设置表信息
     *
     * @param growthSettings
     * @return
     */
    @Override
    public ResponseResult<Integer> growthSettingsInsert(GrowthSettings growthSettings) {
        ResponseResult<Integer> result;

        try {
            int resultCount = userMapper.growthSettingsInsert(growthSettings);
            if (resultCount != 0) {
                result = ResponseResult.success(resultCount);
            } else {
                result = ResponseResult.error();
            }
            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.INSERT_ERROR);
        }
    }

    /**
     * 修改成长模型设置表信息
     *
     * @param growthSettings
     * @return
     */
    @Override
    public ResponseResult<Integer> growthSettingsUpdate(GrowthSettings growthSettings) {
        ResponseResult<Integer> result;

        try {
            int resultCount = userMapper.growthSettingsUpdate(growthSettings);
            if (resultCount != 0) {
                result = ResponseResult.success(resultCount);
            } else {
                result = ResponseResult.error();
            }
            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.UPDATE_ERROR);
        }
    }

    /**
     * 删除成长模型设置表信息
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult<Integer> growthSettingsDelete(int id) {
        ResponseResult<Integer> result;

        try {
            int resultCount = userMapper.growthSettingsDelete(id);
            if (resultCount != 0) {
                result = ResponseResult.success(resultCount);
            } else {
                result = ResponseResult.error();
            }
            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.DELETE_ERROR);
        }
    }

    /**
     * 查询成长模型设置表ID，用于添加模型金额设置表时的填写的SetID
     *
     * @return
     */
    @Override
    public ResponseResult<List<GrowthSettings>> growthSettingsIDSelect() {
        ResponseResult<List<GrowthSettings>> result;


        try {
            List<GrowthSettings> growthSettingsList = userMapper.growthSettingsIDSelect();
            if (growthSettingsList != null) {
                result = ResponseResult.success(growthSettingsList);
            } else {
                result = ResponseResult.error();
            }
            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.SELECT_ERROR);
        }
    }

    /**
     * 查询用户来源信息，用于前端单项查询
     *
     * @return
     */
    @Override
    public ResponseResult<List<UserResource>> userResourceSelect() {
        ResponseResult<List<UserResource>> result;


        try {
            List<UserResource> userList = userMapper.userResourceSelect();

            if (userList != null) {
                result = ResponseResult.success(userList);
            } else {
                result = ResponseResult.error();
            }
            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.SELECT_ERROR);
        }
    }

    /**
     * 添加模型金额设置表信息
     *
     * @param moneySettings
     * @return
     */
    @Override
    public ResponseResult<Integer> moneySettingsInsert(MoneySettings moneySettings) {
        ResponseResult<Integer> result;


        try {
            int resultCount = userMapper.moneySettingsInsert(moneySettings);
            if (resultCount != 0) {
                result = ResponseResult.success(resultCount);
            } else {
                result = ResponseResult.error();
            }

            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.INSERT_ERROR);
        }
    }

    /**
     * 删除模型金额设置表信息
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult<Integer> moneySettingsDelete(int id) {
        ResponseResult<Integer> result;


        try {
            int resultCount = userMapper.moneySettingsDelete(id);
            if (resultCount != 0) {
                result = ResponseResult.success(resultCount);
            } else {
                result = ResponseResult.error();
            }

            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.DELETE_ERROR);
        }
    }

    /**
     * 查询模型金额设置表信息
     *
     * @return
     */
    @Override
    public ResponseResult<List<MoneySettings>> moneySettingsSelect() {
        ResponseResult<List<MoneySettings>> result;

        try {
            List<MoneySettings> moneySettings = userMapper.moneySettingsSelect();

            if (moneySettings != null) {
                result = ResponseResult.success(moneySettings);
            } else {
                result = ResponseResult.error();
            }

            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.SELECT_ERROR);
        }
    }

    /**
     * 更新模型金额设置表信息
     *
     * @param moneySettings
     * @return
     */
    @Override
    public ResponseResult<Integer> moneySettingsUpdate(MoneySettings moneySettings) {
        ResponseResult<Integer> result;


        try {
            int resultCount = userMapper.moneySettingsUpdate(moneySettings);
            if (resultCount != 0) {
                result = ResponseResult.success(resultCount);
            } else {
                result = ResponseResult.error();
            }
            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.UPDATE_ERROR);
        }
    }

    /**
     * 根据用户在前端填写的数据对普通用户信息进行动态查询
     *
     * @param userDynamicQuery
     * @return
     */
    @Override
    public ResponseResult<List<User>> userDynamicQuery(UserDynamicQuery userDynamicQuery) {
        ResponseResult<List<User>> result;

        try {
            List<User> userList = userMapper.userDynamicQuery(userDynamicQuery);
            if (userList != null) {
                result = ResponseResult.success(userList);
            } else {
                result = ResponseResult.error();
            }
            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.SELECT_ERROR);
        }
    }

    /**
     * 根据用户在前端填写的数据对VIP会员信息进行动态查询
     *
     * @param vipuserDynamicQuery
     * @return
     */
    @Override
    public ResponseResult<List<User>> vipuserDynamicQuery(VipuserDynamicQuery vipuserDynamicQuery) {
        ResponseResult<List<User>> result;

        try {
            List<User> userList = userMapper.vipuserDynamicQuery(vipuserDynamicQuery);
            if (userList != null) {
                result = ResponseResult.success(userList);
            } else {
                result = ResponseResult.error();
            }
            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.SELECT_ERROR);
        }
    }

    /////////////////////       rmf模型设置表

    /**
     * 添加rmf模型设置表信息
     *
     * @param rmfModelSettings
     * @return
     */
    @Override
    public ResponseResult<Integer> rmfInsert(RmfModelSettings rmfModelSettings) {
        ResponseResult<Integer> result;


        try {
            int resultCount = userMapper.rmfInsert(rmfModelSettings);
            if (resultCount != 0) {
                result = ResponseResult.success(resultCount);
            } else {
                result = ResponseResult.error();
            }
            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.INSERT_ERROR);
        }
    }

    /**
     * 删除rmf模型设置表信息（逻辑删除）
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult<Integer> rmfDelete(int id) {
        ResponseResult<Integer> result;


        try {
            int resultCount = userMapper.rmfDelete(id);
            if (resultCount != 0) {
                result = ResponseResult.success(resultCount);
            } else {
                result = ResponseResult.error();
            }
            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.DELETE_ERROR);
        }
    }

    /**
     * 查询rmf模型设置表信息
     *
     * @return
     */
    @Override
    public ResponseResult<List<RmfModelSettings>> rmfSelect() {
        ResponseResult<List<RmfModelSettings>> result;


        try {
            List<RmfModelSettings> rmfModelSettings = userMapper.rmfSelect();
            if (rmfModelSettings != null) {
                result = ResponseResult.success(rmfModelSettings);
            } else {
                result = ResponseResult.error();
            }
            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.SELECT_ERROR);
        }
    }

    /**
     * 修改rmf模型设置表信息
     *
     * @param rmfModelSettings
     * @return
     */
    @Override
    public ResponseResult<Integer> rmfUpdate(RmfModelSettings rmfModelSettings) {
        ResponseResult<Integer> result;


        try {
            int resultCount = userMapper.rmfUpdate(rmfModelSettings);
            if (resultCount != 0) {
                result = ResponseResult.success(resultCount);
            } else {
                result = ResponseResult.error();
            }
            return result;

        } catch (Exception e) {
            throw new ServiceException(ResponseCode.UPDATE_ERROR);
        }
    }

    /**
     * 查询订单简要信息
     *
     * @param uid
     * @return
     */
    @Override
    public ResponseResult<List<OrderMessage>> orderSelect(int uid) {
        ResponseResult<List<OrderMessage>> result;
        try {
            List<OrderMessage> orderMessages = userMapper.orderSelect(uid);
            if (orderMessages != null) {
                result = ResponseResult.success(orderMessages);
            } else {
                result = ResponseResult.error();
            }
            return result;
        } catch (Exception e) {
            throw new ServiceException(ResponseCode.SELECT_ERROR);
        }

    }

    /**
     * 查询订单详情信息
     *
     * @param uid
     * @return
     */
    @Override
    public ResponseResult<List<OrderMessage>> orderDetailSelect(int uid) {
        ResponseResult<List<OrderMessage>> result;
        try {
            List<OrderMessage> orderMessages = userMapper.orderDetailSelect(uid);
            if (orderMessages != null) {
                result = ResponseResult.success(orderMessages);
            } else {
                result = ResponseResult.error();
            }
            return result;
        } catch (Exception e) {
            throw new ServiceException(ResponseCode.SELECT_ERROR);
        }
    }

    /**
     * 查询会员积分详情
     *
     * @param uid
     * @return
     */
    @Override
    public ResponseResult<List<Points_details>> pointsDetailSelect(int uid) {
        ResponseResult<List<Points_details>> result;
        try {
            List<Points_details> pointsDetailsList = userMapper.pointsDetailSelect(uid);
            if (pointsDetailsList != null) {
                result = ResponseResult.success(pointsDetailsList);
            } else {
                result = ResponseResult.error();
            }
            return result;
        } catch (Exception e) {
            throw new ServiceException(ResponseCode.SELECT_ERROR);
        }
    }

    /**
     * 查询配送地址信息
     *
     * @param uid
     * @return
     */
    @Override
    public ResponseResult<List<UserAddress>> addrSelect(int uid) {
        ResponseResult<List<UserAddress>> result;
        try {
            List<UserAddress> addressList = userMapper.addrSelect(uid);
            if (addressList != null) {
                result = ResponseResult.success(addressList);
            } else {
                result = ResponseResult.error();
            }
            return result;
        } catch (Exception e) {
            throw new ServiceException(ResponseCode.SELECT_ERROR);
        }
    }
}
